<?php
/**
 * 
 * 
 * $Rev: 7 $
 * $Author: thejahweh $
 * $Date: 2010-09-01 07:30:00 +0000 (Wed, 01 Sep 2010) $
 * $Id: moduleNavigationAdmin.class.php 7 2010-09-01 07:30:00Z thejahweh $
 * $URL: http://allone.googlecode.com/svn/trunk/plugins/navigation/moduleNavigationAdmin.class.php $
 * @author: Adrian Liechti <info@jahweh.ch>
 * @copyright: 2010 Adrian Liechti, GPL3 and CC3 BY-SA
 */

class moduleNavigationAdmin extends module {
	
	public function __construct () {}
	
	public function display () {
		global $navigation;
		/**
		 * Save
		 */
		if (isset($_POST['a']) and $_POST['a'] === 'navigation_save') {
			$saved = false;
			if (isset($_POST['id']) and $_POST['id'] !== NULL) {
				/**
				 * Save Navigation Node
				 */
				$nav = new navigationNode($_POST['id']);
				if ($nav->isLoaded === true and $nav->rightEdit->has() === true) {
					$this->putPostInNode($nav);
					$saved = $nav->save();
				} else {
					/**
					 * TODO Fehler ausgeben, keine Rechte
					 */
				}
			} elseif (isset($_POST['parent_id'])) {
				/**
				 * New Navigation Node
				 */
				$navParentCan = false;
				if ($_POST['parent_id'] === NULL or $_POST['parent_id'] === '') {
					if ($navigation->rightEdit->has() === true) {
						$navParentCan = true;
					}
				} else {
					$navParent = new navigationNode($_POST['parent_id']);
					if ($navParent->isLoaded === true and $navParent->rightEdit->has() === true) {
						$navParentCan = true;
					}
				}
				if ($navParentCan === true) {
					$nav = new navigationNode();
					$this->putPostInNode($nav);
					$saved = $nav->saveNew();
				} else {
					/**
					 * TODO Fehler ausgeben, keine Rechte
					 */
				}
			}
			if ($saved) {
				$navigation->nodesById[$nav->id] = $nav;
			}
		}
		
		/**
		 * GET
		 */
		if (isset($_GET['a'])
			and (($_GET['a'] === 'navigation_edit' and isset($_GET['navigation_id']) 
				and isset($navigation->nodesById[$_GET['navigation_id']]) and $a = true)
			or ($_GET['a'] === 'navigation_new' and isset($_GET['navigation_parent'])
				and ($_GET['navigation_parent'] === NULL or $_GET['navigation_parent'] === ''
					or isset($navigation->nodesById[$_GET['navigation_parent']]))
				and ($a = false) === false))
			)
		{
			$parents = array(NULL=>'NoParent');
			$this->fetchNodes($navigation->nodes, NULL, $parents);
			$this->output->register->templateFunction('url', 'smarty_geturl');
			$this->output->assign('navigation', $navigation);
			$this->output->assign('parents', $parents);
			$this->output->assign('target', navigation::$target);
			$this->output->assign('td', $this->output->pluginDir.'/template/');
			if ($a === true) {
				$this->output->assign('nedit', $navigation->nodesById[$_GET['navigation_id']]);
			} else {
				$newN = new navigationNode();
				$newN->parent_id = $navigation->nodesById[$_GET['navigation_parent']]->id;
				$this->output->assign('nedit', $newN);
			}
			$this->output->display('navigationAdminEdit.module.tpl');
		} else {
			if (isset($_GET['a']) and $_GET['a'] === 'navigation_remove' and 
				isset($_GET['navigation_id']) and isset($navigation->nodesById[$_GET['navigation_id']]))
			{
				$nav = new navigationNode($_POST['id']);
				if ($nav->isLoaded === true and $nav->rightEdit->has() === true) {
					$saved = $nav->remove();
				} else {
					/**
					 * TODO Fehler ausgeben, keine Rechte
					 */
				}
			}
			$this->output->register->templateFunction('url', 'smarty_geturl');
			$this->output->assign('navigation', $navigation);
			$this->output->assign('nodes', $navigation->nodes);
			$this->output->assign('td', $this->output->pluginDir.'/template/');
			$this->output->display('navigationAdmin.module.tpl');
		}
	}
	
	private function fetchNodes(&$nodes, $parent = NULL, &$out, $leftSpan = 0) {
		$span = '- ';
		$s1 = str_repeat($span, $leftSpan);
		foreach ($nodes[$parent] as $p => $n) {
			$out[$n->id] = $s1.htmlSpecialChars($n->name);
			if ($n->id !== NULL and isset($nodes[$n->id])) {
				$this->fetchNodes($nodes, $n->id, $out, $leftSpan+1);
			}
		}
	}
	
	private function putPostInNode (&$nav) {
		if ($_POST['parent_id'] !== NULL)		$nav->parent_id		=& $_POST['parent_id'];
		if ($_POST['name'] !== NULL)			$nav->name			=& $_POST['name'];
		if ($_POST['url'] !== NULL)				$nav->url			=& $_POST['url'];
		if ($_POST['type'] !== NULL)			$nav->type			=& $_POST['type'];
		if ($_POST['highlight'] === '1') {
			$nav->highlight		= '1';
		} else {
			$nav->highlight		= '0';
		}
		if ($_POST['sort'] !== NULL)			$nav->sort			=& $_POST['sort'];
		if ($_POST['target'] !== NULL)			$nav->target		= (in_array($_POST['target'], navigation::$target) ? $_POST['target'] : navigation::$target[0]);
		if ($_POST['template_id'] !== NULL) 	$nav->template_id	=& $_POST['template_id'];
		if ($_POST['layout_id'] !== NULL)		$nav->layout_id		=& $_POST['layout_id'];
		if ($_POST['description'] !== NULL)		$nav->description	=& $_POST['description'];
		if ($_POST['navigation_id'] !== NULL)	$nav->navigation_id	=& $_POST['navigation_id'];
		/**
		 * TODO Rollenzuweisung für die NavigationNodes
		 */
	}
	
}
?>